/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.stop.manage.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.stop.manage.dto.ClosedDriveStopExcelDTO;
import com.hopes.bizz.stop.manage.entity.ClosedDriveStopEntity;
import com.hopes.bizz.stop.manage.service.ClosedDriveStopService;
import com.hopes.bizz.stop.manage.vo.ClosedDriveStopExcelVO;
import com.hopes.bizz.stop.risk.base.dto.VideoDTO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 园区停车场管理
 *
 * @author pig
 * @date 2024-02-18 13:53:23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/park" )
@Tag(description = "park" , name = "园区停车场管理管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ClosedDriveStopController {

    private final ClosedDriveStopService closedDriveStopService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param closedDriveStop 园区停车场管理
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('stop_park_view')" )
    public R getclosedDriveStopPage(Page page, ClosedDriveStopEntity closedDriveStop) {
        LambdaQueryWrapper<ClosedDriveStopEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ObjectUtil.isNotNull(closedDriveStop.getStopType()),ClosedDriveStopEntity::getStopType,closedDriveStop.getStopType());
		wrapper.eq(StrUtil.isNotBlank(closedDriveStop.getStopName()),ClosedDriveStopEntity::getStopName,closedDriveStop.getStopName());
		wrapper.eq(ObjectUtil.isNotNull(closedDriveStop.getUseFlag()),ClosedDriveStopEntity::getUseFlag,closedDriveStop.getUseFlag());
        return R.ok(closedDriveStopService.page(page, wrapper));
    }


    /**
     * 通过id查询园区停车场管理
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('stop_park_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(closedDriveStopService.getById(id));
    }

    /**
     * 新增园区停车场管理
     * @param closedDriveStop 园区停车场管理
     * @return R
     */
    @Operation(summary = "新增园区停车场管理" , description = "新增园区停车场管理" )
    @SysLog("新增园区停车场管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('stop_park_add')" )
    public R save(@RequestBody ClosedDriveStopEntity closedDriveStop) {
        // 新增默认停车位个数为0
        closedDriveStop.setTotalPlace(0);
        closedDriveStop.setSurplusPlace(0);
        return R.ok(closedDriveStopService.save(closedDriveStop));
    }

    /**
     * 修改园区停车场管理
     * @param closedDriveStop 园区停车场管理
     * @return R
     */
    @Operation(summary = "修改园区停车场管理" , description = "修改园区停车场管理" )
    @SysLog("修改园区停车场管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('stop_park_edit')" )
    public R updateById(@RequestBody ClosedDriveStopEntity closedDriveStop) {
        return R.ok(closedDriveStopService.updateById(closedDriveStop));
    }

    /**
     * 通过id删除园区停车场管理
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除园区停车场管理" , description = "通过id删除园区停车场管理" )
    @SysLog("通过id删除园区停车场管理" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('stop_park_del')" )
    public R removeById(@RequestBody Long[] ids) {
        for (Long id : ids) {
            Integer totalPlace = closedDriveStopService.getById(id).getTotalPlace();
            if (!totalPlace.equals(0)){
                return R.failed("停车场存在停车位,不可删除");
            }
        }
        return R.ok(closedDriveStopService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('stop_park_export')")
    public List<ClosedDriveStopExcelVO> template() {
        return closedDriveStopService.exportExcel(Collections.singletonList(new ClosedDriveStopEntity()));
    }

    /**
     * 导出excel 表格
     * @param closedDriveStopExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('stop_park_export')")
    public List<ClosedDriveStopExcelVO> export(ClosedDriveStopExcelDTO closedDriveStopExcelDto) {
        return closedDriveStopService.exportExcel(closedDriveStopService.list());
    }

    /**
     * 导入excel 表格
     * @param closedDriveStopExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('stop_park_export')")
    public R importExcel(@RequestExcel List<ClosedDriveStopExcelDTO> closedDriveStopExcelDtoList, BindingResult bindingResult) {
        return closedDriveStopService.importExcel(closedDriveStopExcelDtoList, bindingResult);
    }

    /**
     * 关联视频
     *
     * @param videoDTO 关联视频信息
     * @return R
     */
    @Operation(summary = "关联视频", description = "关联视频")
    @PutMapping("/video")
    @PreAuthorize("@pms.hasPermission('base_site_edit')")
    public R updateById(@RequestBody VideoDTO videoDTO) {
        if (StringUtils.isNotBlank(videoDTO.getVideoCodes())) {
            ClosedDriveStopEntity closedDriveStop = closedDriveStopService.getById(videoDTO.getId());
            closedDriveStop.setVideoCode(videoDTO.getVideoCodes());
            return R.ok(closedDriveStopService.updateById(closedDriveStop));
        }
        return R.ok();
    }
}
